using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Diagnostics;

namespace CSettingsUI
{
	/// <summary>
	/// Summary description for OptionTabPages.
	/// </summary>
	public class ToolbarOptionPanels : System.Windows.Forms.Form, IPropertyConverterGui
	{
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ErrorProvider errorProvider1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		
		public ToolbarOptionPanels()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			new CSettingsUI.PropertyConverter(new System.Windows.Forms.DataGrid(), this);
			if(this.toolBar1.Buttons.Count>0)
			{
				toolBar1_ButtonClick(this, new ToolBarButtonClickEventArgs(toolBar1.Buttons[0]));					
				toolBar1.Buttons[0].Pushed = true;
			}
			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
			this.SuspendLayout();
			// 
			// toolBar1
			// 
			this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(536, 42);
			this.toolBar1.TabIndex = 0;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// errorProvider1
			// 
			this.errorProvider1.ContainerControl = this;
			// 
			// ToolbarOptionPanels
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(536, 470);
			this.Controls.Add(this.toolBar1);
			this.Name = "ToolbarOptionPanels";
			this.Text = "System.Windows.Forms.DataGrid";
			this.ResumeLayout(false);

		}
		#endregion
	
		#region IPropertyConverterGui Members
		public Control CreateGroup(string group)
		{
			ToolBarButton button = new ToolBarButton(group);
			Panel panel = new Panel();
			//
			button.Tag = panel;
			button.Style = ToolBarButtonStyle.ToggleButton;
			panel.Dock = DockStyle.Fill;
			this.Controls.Add(panel);
			//
			this.toolBar1.Buttons.Add(button);
			//
			return button.Tag as Control;
		}
		public IPropertyConverterControl CreateControl(System.Reflection.PropertyInfo propertyInfo)
		{
            Debug.WriteLine("CreateControl");
			System.Type type = propertyInfo.PropertyType;
			Control ctl = null;
			if(type.Equals(typeof(string)))
			{
				ctl = new TextBoxOption();

			}
			else if((type.BaseType!=null) && (type.BaseType.Equals(typeof(Enum))))
			{
				if(type.GetCustomAttributes(typeof(FlagsAttribute),true).GetLength(0)>0)
				{
					ctl = new CheckedListBoxOption();
				} 
				else
				{
					ctl = new ComboBoxOption();
				}

			}
			else if(type.Equals(typeof(int)))
			{
				ctl = new NumericUpDownOption();
			}
			else if(type.Equals(typeof(DateTime)))
			{
				ctl = new DateTimePickerOption();
			}
			else if(type.Equals(typeof(Decimal)))
			{
				ctl = new NumericUpDownOption();
			}
			else if(type.Equals(typeof(bool)))
			{
				ctl = new CheckBoxOption();
			}
			else if(type.Equals(typeof(Color)))
			{
				ctl = new ColorPickerOption();
			}
            else if (type.IsClass || type.IsInterface)
            {
                ctl = new TextBoxClassOption(); 
                //ctl = new ButtonOption();
            }
			return ctl as IPropertyConverterControl;
		}
		public void SetError(IPropertyConverterControl control, string message)
		{
			this.errorProvider1.SetError((Control) control, message);
		}
		#endregion

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			foreach(ToolBarButton btn in this.toolBar1.Buttons)
			{
				if(btn==e.Button) continue;
				btn.Pushed = false;
			}
			foreach(Control s in this.Controls)
			{
				if(s is System.Windows.Forms.ToolBar) continue;
				s.Visible = false;
			}
			((Control)e.Button.Tag).Visible = true;
			((Control)e.Button.Tag).BringToFront();
		}
		
	}
}
